package no.ntnu.idi.deid.preprocessor.xmlgenerator;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import no.ntnu.idi.deid.documentunits.Token;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XMLTokenGenerator {

	public static void generate(ArrayList<Token> tokens, String path) {
		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

			// root element, document
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("document");
			doc.appendChild(rootElement);

			for (Token token : tokens) {
				System.out.println("kom inn");
				// Token element
				Element tokenElement = doc.createElement("OriginalToken");
				tokenElement.setAttribute("OriginalToken",
						token.getOrginal() + "");
				rootElement.appendChild(tokenElement);

				Element StringInformation = doc
						.createElement("StringInformation");
				StringInformation.setAttribute("Index", token.getIndex() + "");
				tokenElement.appendChild(StringInformation);

				Element strippedToken = doc.createElement("strippedToken");
				strippedToken.appendChild(doc.createTextNode(token
						.getStripped()));
				StringInformation.appendChild(strippedToken);

				Element infoToken = doc.createElement("infoToken");
				infoToken.appendChild(doc.createTextNode(token.getInfoToken()
						+ ""));
				StringInformation.appendChild(infoToken);

				Element specialCharCount = doc
						.createElement("specialCharCount");
				specialCharCount.appendChild(doc.createTextNode(token
						.getSpecialCharCount() + ""));
				StringInformation.appendChild(specialCharCount);

				Element digitCount = doc.createElement("digitCount");
				digitCount.appendChild(doc.createTextNode(token.getDigitCount()
						+ ""));
				StringInformation.appendChild(digitCount);

				Element cleanWord = doc.createElement("cleanWord");
				cleanWord.appendChild(doc.createTextNode(token.isTokenClean()
						+ ""));
				StringInformation.appendChild(cleanWord);

			}

			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File(path));

			// StreamResult result = new StreamResult(System.out);

			transformer.transform(source, result);

			System.out.println("XML-Tokens-File saved in: " + path);

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		}

	}
}
